prices = [7, 1, 5, 3, 6, 4]
//prices = [1, 2]
// var maxProfit = function (prices) {
//     let profit = [0]
//     profit[1] = 0
//     let len = prices.length
//     let day = 2
//     while (day <= len) {
//         profit[day] = 0
//         for (let i = 0; i <= day - 1; i++) {
//             if (prices[i] < prices[day - 1]) {
//                 profit[day] = Math.max(profit[day], prices[day - 1] - prices[i])
//             }
//         }
//         day++
//     }
//     console.log(profit);
//     let max = 0
//     for (let j = 0; j < profit.length; j++) {
//         if (profit[j] > max) {
//             max = profit[j]
//         }
//     }
//     return max
// };
var maxProfit = function (prices) {
    let minprice = Infinity
    let profit = 0
    prices.forEach((item) => {
        minprice = Math.min(item, minprice)
        profit = Math.max(profit, item - minprice)
    })
    return profit
};
console.log(maxProfit(prices));
